﻿using ProTrack.Entity;
using ProTrack.IRepository;
using ProTrack.IServices.ProcessExamine;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace ProTrack.Services.ProcessExamine
{
    public class QcProcessRecordServices : IQcProcessRecordServices
    {
        public QcProcessRecordServices(IBaseRepository db)
        {
            Db = db;
        }

        public IBaseRepository Db { get; }

        //新增
        public async Task<bool> AddAsync(QcProcessRecord model, QcProcessRecordItem model2, QcProcessRecordResult model3)
        {
            var result = false;
            var modelId = await Db.InsertReturnIdAsync(model);
            if (modelId>0)
            {
                model2.RecordId = modelId;
                model3.RecordId = modelId;

                var result2 = await Db.InsertAsync(model2);

                var result3 = await Db.InsertAsync(model3);

                if (result2 && result3)
                {
                    result = true;
                }
            }

            return result;
        }
        //批量删除
        public async Task<bool> DeleteAsync(int[] Ids)
        {
            string sql = "delete QcProcessRecord where Id in @Ids; delete QcProcessRecordItem where RecordId in @Ids; delete QcProcessRecordResult where RecordId in @Ids";
            var result = await Db.DeleteAsync(sql, new { Ids });
            return result;
        }
        //删除
        public async Task<bool> DeleteAsync(int Id)
        {
            string sql = "delete QcProcessRecord where Id = @Id; delete QcProcessRecordItem where RecordId=@Id ; delete QcProcessRecordResult where RecordId=@Id ";
            var result = await Db.DeleteAsync(sql, new { Id });
            return result;
        }


        //获取所有数据
        public async Task<IEnumerable<QcProcessRecord>> GetAllAsync()
        {
            string sql = "select * from QcProcessRecord";
            var list = await Db.QueryAllAsync<QcProcessRecord>(sql);
            return list;
        }
        //根据分页获取所有数据
        public async Task<IEnumerable<QcProcessRecord>> GetAsync(int PageIndex, int PageSize, string RecordCode, string ProcessCode, string ReportUser)
        {
            string sql = "select * from QcProcessRecord where 1=1 ";

            if (!string.IsNullOrEmpty(RecordCode))
            {
                sql += " and RecordCode like @RecordCode ";
            }

            if (!string.IsNullOrEmpty(ProcessCode))
            {
                sql += " and ProcessCode like @ProcessCode ";
            }


            if (!string.IsNullOrEmpty(ReportUser))
            {
                sql += " and ReportUser  = @ReportUser ";
            }

            sql += " order by CreateTime desc offset @PageSize*(@PageIndex-1) row fetch next @PageSize rows only ";

            var list = await Db.QueryAsync<QcProcessRecord>(sql, new { PageIndex, PageSize, RecordCode = $"%{RecordCode}%", ProcessCode = $"%{ProcessCode}%", ReportUser = $"%{ReportUser}%" });
            return list;
        }
        //获取总条数
        public async Task<int> GetCountAsync(string RecordCode, string ProcessCode, string ReportUser)
        {
            string sql = "select count(*) from QcProcessRecord where 1=1 ";
            if (!string.IsNullOrEmpty(RecordCode))
            {
                sql += " and RecordCode like @RecordCode ";
            }

            if (!string.IsNullOrEmpty(ProcessCode))
            {
                sql += " and ProcessCode like @ProcessCode ";
            }
            if (!string.IsNullOrEmpty(ReportUser))
            {
                sql += " and ReportUser  = @ReportUser ";
            }


            var count = await Db.GetRecoreCountAsync(sql, new { RecordCode = $"%{RecordCode}%", ProcessCode = $"%{ProcessCode}%", ReportUser = $"%{ReportUser}%" });
            return count;
        }
        //修改
        public Task<bool> UpdateAsync(QcProcessRecord model, QcProcessRecordItem model2, QcProcessRecordResult model3)
        {
            throw new NotImplementedException();
        }
    }
}
